package com.itmk.web.userInfo.controller;

import com.itmk.utils.JWTUtils;
import com.itmk.utils.Result;
import com.itmk.web.userInfo.entity.*;
import com.itmk.web.userInfo.service.UserInfoService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

//    private static String sign = "admin";

    @Autowired
    UserInfoService userInfoService;

    /**
     * 获取建立信息
     * @param request
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpServletRequest request){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        //根据唯一标识符uuid检索三表信息
        BasicInfo basicInfo = userInfoService.getBasicInfo(uuid);
        EducationInfo educationInfo = userInfoService.getEducationInfo(uuid);
        ForjobInfo forjobInfo = userInfoService.getForJobInfo(uuid);

        //处理educationInfo
        NewEducation newEducation = new NewEducation();
        Undergo undergo = new Undergo();

        undergo.setStart(educationInfo.getUndergo_start());
        undergo.setEnd(educationInfo.getUndergo_end());

        newEducation.setBackground(educationInfo.getBackground());
        newEducation.setType(educationInfo.getType());
        newEducation.setSchool(educationInfo.getSchool());
        newEducation.setMajor(educationInfo.getMajor());
        newEducation.setExperience(educationInfo.getExperience());
        newEducation.setUuid(educationInfo.getUuid());
        newEducation.setUndergo(undergo);
        //整合
        UserInfo userInfo = new UserInfo();
        userInfo.setBasicInfo(basicInfo);
        userInfo.setEducationInfo(newEducation);
        userInfo.setForjobInfo(forjobInfo);

        return Result.ok().put("data",userInfo);
    }


    /**
     * 修改用户个人头像
     * @param request
     * @param profile
     * @return
     */
    @RequestMapping("/updateUserProfile")
    @ResponseBody
    public Result updateUserLogo(HttpServletRequest request,MultipartFile profile){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        if(profile!=null){
            //file重命名
            String originalFilename = profile.getOriginalFilename();
            String ext = "."+originalFilename.split("\\.")[1];
            String imageUUID = UUID.randomUUID().toString().replace("-","");
            String fileName = imageUUID+ext;
            //上传图片
//            ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//            String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
//                    "\\src\\main\\resources\\static\\images\\companyLogo\\";
            String property = System.getProperty("user.dir")+"\\itmk-base-web";
            String pre = property + "\\target\\classes\\static\\images\\userProfile\\";

            userInfoService.updateLogo(fileName,uuid);
            String path1 = pre+fileName;
            String path = path1.replace("/","\\");
            try {
                profile.transferTo(new File(path));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return Result.ok("修改成功").put("profile",fileName);
        }
        return Result.error("上传图片失败");
    }


    /**
     * 修改基本信息
     * @param request
     * @param basicInfo
     * @return
     */
    @RequestMapping("/updateBasicInfo")
    @ResponseBody
    public Result updateBasicInfo(HttpServletRequest request, @RequestBody BasicInfo basicInfo){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        Boolean result = userInfoService.updateBasicInfo(basicInfo,uuid);
        if(result){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 修改EducationInfo表
     * @param request
     * @return
     */
    @RequestMapping("/updateEducationInfo")
    @ResponseBody
    public Result updateEducationInfo(HttpServletRequest request, @RequestBody EducationInfo edi){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        Boolean result = userInfoService.updateEdI(edi,uuid);
        if (result){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改EducationInfo表
     * @param request
     * @return
     */
    @RequestMapping("/updateForjobInfo")
    @ResponseBody
    public Result updateForJobInfo(HttpServletRequest request, @RequestBody ForjobInfo fji){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        Boolean result = userInfoService.updateFji(fji,uuid);
        if (result){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }
}
